/**
 * Created by python on 19-3-1.
 */
let app = new Vue({
    el: "#app",
    data: {
        // 页面中需要使用到的数据，键值对
        title_text:"",
        user_id: localStorage.user_id || sessionStorage.user_id,
        token: localStorage.token || sessionStorage.token,
        tags_text:"",
        HOST: HOST,
        content_text:"",
        token: sessionStorage.token || localStorage.token,
    },
    mounted: function () {
        CKEDITOR.replace('editor1');
        // 一加载就需要做的，直接是代码
        if(localStorage.getItem('title_text')){
            this.title_text = localStorage.getItem('title_text')
        }
        if(localStorage.getItem('tags_text')){
            this.tags_text = localStorage.getItem('tags_text')
        }
        if(localStorage.getItem('content_text')){
            // alert('1')
            // alert($("iframe").contents().find("body").children().text(localStorage.getItem('content_text')))
            setTimeout(() => {
                $("iframe").contents().find("body").children().text(localStorage.getItem('content_text'))
        }, 1000);
        }
        setInterval(() => {
            localStorage.setItem('content_text',CKEDITOR.instances.editor1.document.getBody().getText())
            // alert($("iframe").contents().find("body").children().html())
            // alert($("iframe").contents().find("body").html())
        }, 1000);

        // if(localStorage.getItem('body_content')){
        //     CKEDITOR.instances.editor1.document.SetBody()
        // }
        // this.labels_text = localStorage.getItem('labels_text')
        // console.log(localStorage.getItem('title_text'))

    },

    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
        submit: function(){
            // alert(1)
            this.body_content=CKEDITOR.instances.editor1.document.getBody().getText();
            if (this.body_content){
                axios.post(HOST+'/discussion/create/', {
                    content:this.body_content,
                    title:this.title_text,
                    labels:this.tags_text,
                    user_id:this.user_id
                    },{
                        // headers: {
                        //     'Authorization': 'JWT ' + this.token
                        // },
                        responseType: 'json'
                    })
                    .then(response => {
                        // alert(response.data['message'])

                            location.href = '/qa-login.html'

                    })
                    .catch(error => {
                        location.href = '/qa-login.html'

                });
            }
        },

    },
    computed: {
        // 需要通过计算得到的数据，键值对 ，键是名称，值是匿名函数
    },
    watch:{
        title_text(n,m){
            if(n!=m){
                // console.log(n,m)
                localStorage.setItem('title_text',n)
            }
        },
        tags_text(n,m){
            if(n!=m){
                localStorage.setItem('tags_text',n)
            }
        }
    },
})